package com.pcbly.web.persistence.calendar;

/**
 * @author carl.song
 * Copyright (c) 2010 Lightsoft, International.
 * All rights reserved.
 *
 */
import java.util.List;

import org.hibernate.Session;

import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.bean.calendar.CalendarEventBean;
import com.pcbly.web.persistence.HibernateBaseDao;
import com.pcbly.web.components.hibernate.support.Page;

@SuppressWarnings("unchecked")
public class CalendarEventHibernateDao extends
		HibernateBaseDao<CalendarEventBean> implements CalendarEventDao {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8846928831827617015L;

	protected Class<CalendarEventBean> getEntityClass() {
		return CalendarEventBean.class;
	}

	public CalendarEventHibernateDao() {
		super();
	}

	@Override
	public CalendarEventBean saveCalendarEventBean(CalendarEventBean calendar)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug(calendar.getId());
		}
		String id = (new GUID()).toString();
		calendar.setId(id);
		getHibernateTemplate().save(calendar);
		return calendar;
	}

	@Override
	public void removeCalendarEventBeanByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		ids = ids.replaceAll(",", "','");
		String hsql = "update t_cal_event set RECYCLED='1' where ID in( '"
				+ ids + "')";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	@Override
	public void removeCalendarEventBeanById(String id) throws Exception {
		// TODO Auto-generated method stub
		id = id.replaceAll(",", "','");
		String hsql = "update t_cal_event set RECYCLED='1' where ID= '"
				+ id + "'";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	public Page<CalendarEventBean> findCalendarEventBeanByCond(
			CalendarEventBean bean, Page<CalendarEventBean> page,
			String orderByName, boolean asc) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from t_cal_event where RECYCLED='0' ");
		if (!(bean == null)) {
		}
		if (asc) {
			// criteria.addOrder(Order.asc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" ");
		} else {
			// criteria.addOrder(Order.desc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" desc ");
		}
		Object obj = getSession().createSQLQuery(
				"select count(t.ID) icount from ("
						+ removeSelect(removeOrders(sb.toString())) + ")t")
				.uniqueResult();
		if (obj == null) {
			obj = "0";
		}

		page.setTotalCount(Integer.parseInt(String.valueOf(obj)));
		if (page.getTotalCount() > 0) {
			int first = page.getFirst();
			int lpage = first + page.getPageSize();
			if (lpage > page.getTotalCount()) {
				lpage = page.getTotalCount();
			}
			List<CalendarEventBean> beans = getSession().createSQLQuery(
					sb.toString()).addEntity(CalendarEventBean.class).list()
					.subList(first, lpage);
			page.setResult(beans);
		}
		return page;
	}

	@Override
	public CalendarEventBean updateCalendarEventBeanDoneFlagById(String id, String doneFlag)
			throws Exception {
		// TODO Auto-generated method stub
		Session session=getSession();
		CalendarEventBean event=(CalendarEventBean)session.load(CalendarEventBean.class,id);
		event.setDoneFlag(doneFlag);
		session.update(event);
		return event;
		
	}
}